// packageE/goods/goods.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],
  properties: {
    datas: {
      type: null,
    },
    page_id: {
      type: null,
    },
    component_id: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    U_tabcontrol: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    isLast: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    integral: "积分",
    emptyImage:  `${app.globalData.imgPath}image.png`,
    clientWidth: "375",
    list: [],
    language: "",
    goodsShow: true,

    page: 1,
    total_page: 0,
    isLoadMore: true,
    allLoaded: false, // 全部数据已经加载完成
    imgPath: app.globalData.imgPath,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en
      });

      let value = wx.getStorageSync("integral");
      if (value) {
        this.setData({
          integral: value,
        });
      }
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
      // list: this.data.datas.list.data ?
      //   this.data.datas.list.data : this.data.datas.list,
    });

    this._getLocation(() => {
      this.getdata();
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    gotoDetail(e) {
      let id = e.currentTarget.dataset.item.goodid;
      wx.navigateTo({
        url: "/packageA/detail_v2/detail_v2?id=" + id,
      });
    },
    // 获取更多数据
    getMoreData() {
      let that = this;
      that.setData({
        isLoadMore: false,
      });
      if (that.data.page >= that.data.last_page) {
        return;
      } else {
        that.setData({
          page: that.data.page + 1,
        });
        let ressa = wx.getStorageSync("address-point");
        let urlStr = app.getNetAddresss(
          "plugin.store-projects.frontend.designer.get-goods"
        );
        let point = app.qqMapTransBMap(
          parseFloat(ressa.point.lng),
          parseFloat(ressa.point.lat)
        );
        urlStr += "&decorate_id=" + that.data.page_id;
        urlStr += "&component_id=" + that.data.component_id;
        urlStr += "&page=" + that.data.page;
        urlStr += "&lng=" + point.lng;
        urlStr += "&lat=" + point.lat;
        urlStr += "&city_name=" + ressa.city;
        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              that.setData({
                last_page: res.data.last_page,
              });

              if (that.data.page < that.data.last_page) {
                that.setData({
                  isLoadMore: true,
                });
              } else {
                that.setData({
                  allLoaded: true,
                });
              }

              let data = that.data.list.concat(res.data.data);
              // if(data.length > this.data.datas.get_number) {
              // 	data.splice(this.data.datas.get_number, data.length-this.data.datas.get_number);
              // 	that.setData({
              // 		isLoadMore: false,
              // 		allLoaded: true
              // 	})
              // }

              that.setData({
                list: data,
              });
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false,
                allLoaded: true,
              });
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    },
    getdata() {
      var that = this;
      let ressa = wx.getStorageSync("address-point");
      let point = {};
      let urlStr = app.getNetAddresss(
        "plugin.store-projects.frontend.designer.get-goods"
      );
      if (ressa) {
        point = app.qqMapTransBMap(
          parseFloat(ressa.point.lng),
          parseFloat(ressa.point.lat)
        );
      } else {
        point = {
          lng: "",
          lat: ""
        };
      }

      urlStr += "&decorate_id=" + that.data.page_id;
      urlStr += "&component_id=" + that.data.component_id;
      urlStr += "&page=" + that.data.page;
      urlStr += "&lng=" + point.lng;
      urlStr += "&lat=" + point.lat;
      urlStr += "&city_name=" + ressa.city;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: false,
              last_page: res.data.last_page,
            });
            if (res.data.current_page < res.data.last_page) {
              that.setData({
                isLoadMore: true,
              });
            } else {
              that.setData({
                allLoaded: true,
              });
            }

            // if(res.data.data.length > this.data.datas.get_number) {
            // 	res.data.data.splice(this.data.datas.get_number, res.data.data.length-this.data.datas.get_number);
            // 	that.setData({
            // 		isLoadMore: false,
            // 		allLoaded: true
            // 	})
            // }

            that.setData({
              list: res.data.data,
            });
            // 跟无商品的时候图片冲突了
            // if (res.data.data <= 0) {
            //   that.setData({
            //     goodsShow: false,
            //   });
            // }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
              allLoaded: true,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
  },
  observers: {
    bottomShowBol: function (flag) {
      if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.datas.sort_style.choose_icon != 5 && this.data.isLoadMore) {
        this.getMoreData();
      }
    },
  },
});